<?php


namespace app\admin\controller;


use think\facade\Db;

class System extends AdminBase
{
    public function config_index(){
        $model=Db::name('system')
            ->where('id',1)
            ->find();
        return view('system_base',['model'=>$model]);
    }
    public function config_add(){
        $data=input('post.');
        unset($data['editorValue']);
        $res=Db::name('system')
            ->update([
                'id'=>1,
                'xiaohongshu'=>$data['xiaohongshu'],
                'weibo'=>$data['weibo'],
                'wechat'=>$data['wechat'],
                'ios'=>$data['ios'],
                'android'=>$data['android'],
                'xiaochengxu'=>$data['xiaochengxu'],
                'version'=>$data['version'],
                'wechat_img'=>$data['wechat_img'],
            ]);
        if (!$res){
            return show(0,'保存失败');
        }
        return show(1,'保存成功');
    }
    public function banner_index()
    {
        $model=Db::name('banner')
            ->order(['status'=>'ASC','sort'=>'DESC'])
            ->select()
            ->toArray();
        foreach ($model as $k=>$v){
            $model[$k]['type']=\app\common\model\Banner::TYPE[$v['type']];
        }
        return view("banner_list",['model'=>$model]);
    }
    public function banner_status(){
        $data=input('post.');
        $model=Db::name('banner')
            ->where('id',$data['id'])
            ->update(['status'=>$data['status']]);
        if (!$model){
            return show(0);
        }
        return show(1);
    }

    public function banner_add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data['id']) {
                $data['update_time']=time();
                $res = Db::name('banner')
                    ->where('id', $data['id'])
                    ->update($data);
            } else {
                $data['create_time']=time();
                $data['update_time']=time();
                $res = Db::name('banner')
                    ->insert($data);
            }
            if (!$res) {
                return show(0, '数据保存失败');
            }
            return show(1, '数据保存成功');
        } else {
            $id = input('id');
            $model = Db::name('banner')
                ->where('id', $id)
                ->find();
            $type = \app\common\model\Banner::TYPE;
            return view('banner_add', ['model' => $model, 'type' => $type]);
        }
    }
}